package com.itheima.day10.myStream.test4;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class Demo {
    public static void main(String[] args) {
//        Stream流中常用的中间方法
        ArrayList<String> al = new ArrayList<>(List.of("张三", "张三丰", "王爱平", "张无忌", "王爱平", "李世明", "王健林", "王爱平"));

//        Stream<T> limit(long maxSize): 截取指定参数个数的数据
        // method1(al);

//        Stream<T> skip(long n): 跳过指定参数个数的数据
        // method2(al);

//        static <T> Stream<T> concat(Stream a,Stream b): 合并a和b两个流为一个流
        // method3(al);

//        Stream<T> distinct(): 去除流中重复的元素(依赖hashCode和equals方法)
        // method4(al);

    }

    private static void method4(ArrayList<String> al) {
        al.stream().distinct().forEach(s -> System.out.println(s));
    }

    private static void method3(ArrayList<String> al) {
        ArrayList<String> al2 = new ArrayList<>(List.of("哈哈", "张三", "张三丰", "张无忌", "李世明", "王健林", "王爱平"));
        /*Stream<String> stream1 = al.stream();
        Stream<String> stream2 = al2.stream();
        Stream<String> concat = Stream.concat(stream1, stream2);
        concat.forEach(s -> System.out.println(s));*/
        Stream.concat(al.stream(), al2.stream()).forEach(s -> System.out.println(s));
    }

    private static void method2(ArrayList<String> al) {
        al.stream().skip(3).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.print(s + " ");
            }
        });
        System.out.println();
        al.stream().skip(3).forEach(s -> System.out.print(s + " "));
    }

    private static void method1(ArrayList<String> al) {
        al.stream().limit(3).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.print(s + " ");
            }
        });
        System.out.println();
        al.stream().limit(3).forEach(s -> System.out.println(s));
    }
}
